package samples.examples.div_clock.views;


import divtastic.js.DisplayDiv;
import divtastic.js.Drawing;
import divtastic.js.application.views.WindowView;
import zpartanlite.Enumerables;
import js.Lib;

using divtastic.js.Drawing;
using samples.examples.div_clock.views.DivClockView;
class DivClockView
{
    
    
    
    private var _holder:        DisplayDiv;
    private var _win:           WindowView;
    
    //These need to be Divtastic's to allow rotation
    private var _hourHand:      DisplayDiv;
    private var _minHand:       DisplayDiv;
    private var _secHand:       DisplayDiv;
    private var _sec:           Int;
    private var _min:           Int;
    private var _hour:          Int;
    private var origTitle:      String;
    
    
    public function new( holder_: DisplayDiv, win_: WindowView )
    {
        
        _holder     = holder_;
        _win        = win_;
        _hour       = -1;
        _min        = -1;
        _sec        = -1;
        var dGap    = 10;
        var hGap    = Std.int( WindowView.headerHeight + dGap );
        var w       = Std.int( _holder.width - dGap*2 );
        var h       = Std.int( _holder.height - hGap - dGap );
        
        _holder.drawGradElipse  (   Std.int( dGap )
                                ,   hGap 
                                ,   w
                                ,   h
                                ,   0xebe9eb
                                ,   0xf7e9f2
                                ,   1
                                ,   Vertical
                                );
        var cx      = dGap + w/2;
        var cy      = hGap + h/2;
        origTitle   = _win.title;
        _hourHand   = new DisplayDiv();
        _hourHand.x = cx;
        _hourHand.y = cy;
        _hourHand.drawHand( 21, Std.int( ( h/2 - dGap )/1.8 ),  0x333333,0x333333 );
        _holder.addChild( _hourHand );
        
        _minHand    = new DisplayDiv();
        _minHand.x  = cx;
        _minHand.y  = cy;
        _minHand.drawHand( 15, Std.int( h/2 - dGap ), 0xd67297,0xd67297 );
        _holder.addChild( _minHand );
        
        _secHand    = new DisplayDiv();
        _secHand.x  = cx;
        _secHand.y  = cy;
        _secHand.drawHand( 8, Std.int( h/2 - dGap/2 ), 0x9d9e6a,0x9d9e6a );
        _holder.addChild( _secHand );
        
        
        var timer = new haxe.Timer(1000);
        timer.run = updateRotation;
        updateRotation();
        
    }
    
    
    public function updateRotation()
    {
        
        var currTime    = Date.now();
        _sec            = currTime.getSeconds();
        var min         = currTime.getMinutes();
        var hour        = currTime.getHours();
        
        if( hour != _hour )
        {
            
            _hourHand.rotation = (360/12)*hour%12 ;
            
        }
        
        if( min != _min )
        {
            
            _minHand.rotation = (360/60)*min ;
            
        }
        
        _secHand.rotation   = (360/60)*_sec ;
        _min                = min;
        _hour               = hour;
        _win.title = origTitle + '  - ' + _hour + ':' + _min + ':' + _sec;
        
    }
    
    
    //use
    private static function drawHand( scope: DisplayDiv, width, length, c0: Int, c1: Int  )
    {
        
        var inside      = new DisplayDiv();
        inside.x        = -width/2;
        inside.y        = -length;
        scope.addChild( inside );
        
        inside.drawGradTriangle(  0, 0, width, length, c0, c1, North, 1, Vertical );
        
        var smaller     = Std.int( length / 4 );
        
        inside.drawGradTriangle(  0, length, width, smaller, c0, c1, South, 1, Vertical );
        
    }
    
    
}